package com.tmz.cloudmall.cloudmallgateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @ClassName RequestLoggingFilter
 * @Description 这里填写类的功能描述
 * @Author tmz
 * @Date 2025/9/2
 */
@Slf4j
@Component
public class RequestLoggingFilter  implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        log.info("请求进入Gateway，原始路径: {}", request.getURI().getPath());
        return chain.filter(exchange).then(
                Mono.fromRunnable(() -> {
                    ServerHttpRequest finalRequest = exchange.getRequest();
                    log.info("请求经过过滤器后，路径: {}", finalRequest.getURI().getPath());
                })
        );
    }

    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}